﻿using Infrastructures;
using IRepository;
using Models;
using Spring.Data.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryMySQL
{
    public class LogRepository : ILogRepository
    {
        public void Create(EventLog log)
        {
            string strSql = "insert into EventLog(InstanceID,EventID,ActionType,ActionObject,ActionTime,Operator,Remark) "
                + " values(@instance_id, @event_id, @action_type, @action_object, @action_time, @operator, @remark)";

            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();
            p.Add("instance_id", DbType.String).Value = log.InstanceID;
            p.Add("event_id", DbType.String).Value = log.EventID;
            p.Add("action_type", DbType.String).Value = log.ActionType.ToString();
            p.Add("action_object", DbType.String).Value = log.ActionObject;
            p.Add("action_time", DbType.DateTime).Value = log.ActionTime;
            p.Add("operator", DbType.String).Value = log.Operator;

            int max = Convert.ToInt32(SystemConfig.Get("MaxLogLength"));
            if (log.Remark.Length > max)
                log.Remark = log.Remark.Substring(0, max);

            p.Add("remark", DbType.String).Value = log.Remark;

            DBHelper.TemplateMySQL.ExecuteNonQuery(CommandType.Text, strSql, p);
        }

        public List<EventLog> List(string eventID)
        {
            throw new NotImplementedException();
        }

        public List<EventLog> List(EventType type, string eventObj)
        {
            throw new NotImplementedException();
        }
    }
}
